
namespace AlertMail
{
    using System;
    using System.Data;
    
    interface IJobSeekerAlert
    {
        void SendEmailToJobSeekers(string schedule);
    }

    public class JobSeekerAlert : IJobSeekerAlert
    {
        public JobSeekerAlert()
        { 
             
        }

        
        public void SendEmailToJobSeekers(string schedule)
        {
            try
                {
                SqlServerProvider sqlServer = new 
                SqlServerProvider();
                sqlServer.CommandText = 
                "select * from tblJSJobAlerts"+
                " where Activate=1 and " +
                "Schedule='" + schedule + "'";
                DataTable dataTableAlert =  
                sqlServer.GetDataTable();                
                string keyWord = "";
                string location = "";
                string category = "0";
                string industry = "0";
                string emailAddress = "";
                DataTable dataTable;
                if (dataTableAlert.Rows.Count > 0)
                {
                    for(int i = 0 ; i < dataTableAlert.Rows.Count; i++)
                    {                        
                        keyWord = dataTableAlert.Rows[i]["KeyWord"].ToString();
                        location = dataTableAlert.Rows[i]["CityName"].ToString();
                        category = dataTableAlert.Rows[i]["JobCategoryID"].ToString();
                        industry = dataTableAlert.Rows[i]["JobIndustryID"].ToString();
                        emailAddress = dataTableAlert.Rows[i]["Email"].ToString();
                        dataTable = GetJobs(keyWord, location, category, industry);
                        string contentToSend = "";
                        if(dataTable  != null)
                            contentToSend = CreateContentForJobSeeker(dataTable);
                        if(contentToSend != "")
                            SendMailToJobSeeker(emailAddress, keyWord, contentToSend);
                    }
                } 
                sqlServer.CloseConnection();  
             }
             catch (Exception ex)
             {
                throw new ApplicationException("JobSeeker::SendEmails", ex);
             }           
        }
        private void SendMailToJobSeeker(string emailTo, string subject, string contentToSend)
        {
            MailProvider mailProvider = new MailProvider();
            mailProvider.SendMail(emailTo, subject, contentToSend);
        }
        private string CreateContentForJobSeeker(DataTable dataTable)
        {
            string siteUrl = 
            "<a href=\"http://www.recruitvietnam.com/"+
            "jobseekers/jobdetails.aspx?";
            string content = "Dear JobSeeker\n";
            content += "RecruitVietnam send "+
            "alert jobs for you to reference. \n";
            
            try
            {
                content += "<table border=0 width=100%>";
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    content += "<tr><td>";
                    content = siteUrl + "JobID="
                    + dataTable.Rows[i]["JobID"].ToString()
                    + "&RecruiterID=" +
                    dataTable.Rows[i]["RecruiterID"].ToString()
                    + " \">" +
                    dataTable.Rows[i]["JobTitle"].ToString()
                    + "</a>" + "</td><td>" +
                    dataTable.Rows[i]["WorkLocation"].ToString()
                    + "</td><td>" +
                    dataTable.Rows[i]["JobCategoryName"].ToString() + "</td></tr>";
                }
                content += "</table>";
            }
            catch (Exception ex)
            {
                throw new ApplicationException("JobSeeker::CreateContentForJobSeeker", ex);
            } 
            return content;
        }
        private DataTable GetJobs(string keyWord, 
        string location, string category, 
        string industry)
        {
            DataTable dataTable = null;
            try
            {
                SqlServerProvider sqlServerProvider =
                    new SqlServerProvider();             

                sqlServerProvider.ParameterCollection
                = new string[4] 
                {"@Keyword", "@WorkLocation", 
                "@Category", "@Industry"};
                sqlServerProvider.ValueCollection
                = new string[4] 
                { "%" + keyWord + "%", "%" + 
                location + "%", category, 
                industry};
                sqlServerProvider.CommandType =
                CommandType.StoredProcedure;

                sqlServerProvider.CommandText =
                "SPGetJobAlerts";
                dataTable =
                sqlServerProvider.GetDataTable();
            }
            catch(Exception ex)
            {
                throw new ApplicationException("JobSeeker::GetJobs", ex);
            }
            return dataTable;
        }
    }
}
